#include "Encryptor.h"
#include "stdlib.h"

Encryptor::Encryptor(void)
{
}

Encryptor::~Encryptor(void)
{
}

wchar_t* Encryptor::encrypt(wchar_t* str, char* key){
	wchar_t* res = new wchar_t[1024];
	int key_res = 0;
	for(int i = 0; key[i] != '\0'; i++)
		key_res += key[i];
	key_res = key_res % 127;
		
	int i = 0;
	for(i = 0; str[i]!= '\0';i++){
		res[i] = (str[i] + key_res)%127 + 1;
	}
	res[i] = '\0';
	
	return res;
}

wchar_t* Encryptor::decrypt(wchar_t* str, char* key){
	wchar_t* res = new wchar_t[1024];
	int key_res = 0;
	for(int i = 0; key[i] != '\0'; i++)
		key_res += key[i];
	key_res = key_res % 127;		
	int i = 0;
	for(i = 0; str[i]!= '\0';i++){
		res[i] = (str[i] +127 - key_res)%127 - 1;
	}
	res[i] = '\0';	
	return res;
}

void Encryptor::gen_random(char *s, const int len) {
    static const char alphanum[] =
        //"0123456789"        
        "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    s[len] = '\0';
}